package cn.ftx.service.impl;

import cn.ftx.entity.Fangtianxia;
import cn.ftx.mapper.FangtianxiaMapper;
import cn.ftx.model.*;
import cn.ftx.service.FangtianxiaService;
import cn.ftx.utils.ArithmeticUtils;
import cn.ftx.utils.ConstantUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AUTHER
 * @since 2021-04-29
 */
@Service
public class FangtianxiaServiceImpl extends ServiceImpl<FangtianxiaMapper, Fangtianxia> implements FangtianxiaService {

    @Autowired
    private FangtianxiaMapper fangtianxiaMapper;

    @Override
    public List<FangtianxiaDTO> queryHouseList(Integer number) {
        String sql = "limit 0," + number;
        QueryWrapper<Fangtianxia> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last(sql);
        List<Fangtianxia> fangtianxias = fangtianxiaMapper.selectList(queryWrapper);
        return fangtianxias.stream().map(r -> {
            FangtianxiaDTO fangtianxiaDTO = new FangtianxiaDTO();
            BeanUtils.copyProperties(r, fangtianxiaDTO);
            return fangtianxiaDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HouseTypeVO> queryHouseTypeRatio() {
        List<HouseTypeDTO> typeDTOList = fangtianxiaMapper.queryHouseTypeRatio();
        Integer sum = typeDTOList.stream().mapToInt(HouseTypeDTO::getNumber).sum();
        return typeDTOList.stream().map(r -> {
            HouseTypeVO houseTypeVO = new HouseTypeVO();
            houseTypeVO.setType(r.getType());
            houseTypeVO.setNumber(r.getNumber());
            double div = ArithmeticUtils.div(r.getNumber(), sum, ConstantUtil.INT_FOUR.intValue());
            double mul = ArithmeticUtils.mul(div, ConstantUtil.DOUBLE_HUNDRED, ConstantUtil.INT_FOUR);
            houseTypeVO.setRatio(mul);
            return houseTypeVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HouseRankVO> queryHouseRankRatio() {
        List<HouseRankDTO> rankDTOS = fangtianxiaMapper.queryHouseRankRatio();
        return rankDTOS.stream().map(r -> {
                    HouseRankVO houseRankVO = new HouseRankVO();
                    BeanUtils.copyProperties(r, houseRankVO);
                    return houseRankVO;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public List<Double> queryHouseAreaList() {
        return fangtianxiaMapper.queryHouseAreaList();
    }

    @Override
    public List<HouseAreaPriceVO> queryHouseAreaPriceList() {
        return fangtianxiaMapper.queryHouseAreaPriceList();
    }

    @Override
    public List<String> queryHouseYearList() {
        return fangtianxiaMapper.queryHouseYearList();
    }

    @Override
    public List<HouseYearPriceVO> queryYearPriceList() {
        return fangtianxiaMapper.queryYearPriceList();
    }

    @Override
    public List<HouseYearTypeVO> queryYearTypeList() {
        List<HouseYearTypeVO> houseYearTypeVOS = new ArrayList<>();

        List<HouseYearTypeDTO> houseYearTypeDTOS = fangtianxiaMapper.queryYearTypeList();
        Map<String, List<HouseYearTypeDTO>> yearMap = houseYearTypeDTOS.stream().collect(Collectors.groupingBy(HouseYearTypeDTO::getBuildTime));

        List<String> yearList = fangtianxiaMapper.queryHouseYearList();
        for (String r : yearList) {
            if (yearMap.containsKey(r)) {
                List<HouseYearTypeDTO> yearTypeDTOS = yearMap.get(r);
                HouseYearTypeDTO yearTypeDTO = yearTypeDTOS.stream().max(Comparator.comparingInt(HouseYearTypeDTO::getNumber)).get();
                HouseYearTypeVO yearTypeVO = new HouseYearTypeVO();
                BeanUtils.copyProperties(yearTypeDTO, yearTypeVO);
                houseYearTypeVOS.add(yearTypeVO);
            }
        }
        return houseYearTypeVOS;
    }

    @Override
    public HouseTypePriceVO queryTypePriceList() {
        HouseTypePriceVO houseTypePriceVO = new HouseTypePriceVO();
        List<String> buildTime = new ArrayList<>();
        List<Double> maxPrice = new ArrayList<>();
        List<Double> minPrice = new ArrayList<>();
        List<Double> avgPrice = new ArrayList<>();
        List<HouseTypePriceDTO> typePriceDTOS = fangtianxiaMapper.queryTypePriceList();
        typePriceDTOS.stream().forEach(r -> {
            buildTime.add(r.getBuildTime());
            maxPrice.add(r.getMaxPrice());
            minPrice.add(r.getMinPrice());
            avgPrice.add(r.getAvgPrice());
        });
        houseTypePriceVO.setBuildTime(buildTime);
        houseTypePriceVO.setAvgPrice(avgPrice);
        houseTypePriceVO.setMaxPrice(maxPrice);
        houseTypePriceVO.setMinPrice(minPrice);
        return houseTypePriceVO;
    }
}
